const App = getApp();
import MYJS from '../../utils/myinit.js';
Page({
    data: {
      buyid:0,
      YU:'',
      fromShare:'no',
      tiankongDA:"",
      isXuanArr:[0,0,0,0,0,0],
      zimuArr:['A','B','C','D','E','F'],
      keyArr:['pq_ask1','pq_ask2','pq_ask3','pq_ask4','pq_ask5','pq_ask6'],
      xuanzeTip: '', // 选择答案之后的文字提示
      isRight:'', // yes no
      isRightStr: '', // 当前问卷回答是否正确
      nowDaanTip: '', // 每次回答后，展示回答是否正确，如果错误，提示正确答案
      nextQaResult:null,

      show_answerNum:0,
      show_baifenbi:0,

      buyInfo:null,   // 试卷订单信息
      perKaoObj:null, // 本次考试信息
      nowPerQution:null, //当前考题信息
      nowBL: 0,   // 当前做题的百分比
      lastSecond: 300 * 1000,  //每个题给五分总倒计时
    },
    onLoad: function(options) {
      let _this = this;
      _this.setData({YU:App.YU, buyid:options.buyid, fromShare:options.fromShare});
      // 设置navbar标题、颜色
      App.setNavigationBar();
      this.getQaInfo();
    },
    /** 生命周期函数--监听页面显示 */
    onShow() {

    },
    /** 获取试卷信息 */
    getQaInfo(){
      let _this = this;
      App._get('beganswer/getQaBoxInfo', { buyid: _this.data.buyid }, result => {
        let _buyInfo = result.data.buyInfo,
        _perKaoObj = result.data.perKaoObj;
        _this.setData({perKaoObj:_perKaoObj});
        //console.log(result);

        // 开始展示当前的考题
        _this.getPerQuesion();
      });
    },
    /** 获取本题的信息 */
    getPerQuesion(){
      let _this = this;
      App._get('beganswer/getPerQuestion', { exid: this.data.perKaoObj.ex_id, answerNum:this.data.perKaoObj.ex_answer_num}, result => {
        //console.log(result);
        _this.opmTheQuestionData(result);
      });
    },
    /** 处理获得的答题数据 */
    opmTheQuestionData(result){
      let _this = this;
      var bifenbi = Math.floor((result.data.kaoshiObj.ex_answer_num+1)*100/ result.data.kaoshiObj.ex_qa_num)
      _this.setData({nowPerQution:result.data.perQuestion, 
        show_answerNum:result.data.kaoshiObj.ex_answer_num+1,
        show_baifenbi:bifenbi,
        isXuanArr:[0,0,0,0,0,0],
        xuanzeTip:"",
        nowDaanTip:"", isRight:"", nextQaResult:null, //初始化正确错误答案信息
        perKaoObj:result.data.kaoshiObj});
        
        this.selectComponent('.countDown').reset(); //重置时间倒计时计数器
    },
    /** 点击了每个选项 */
    xuanzeFun(e){
      let cnt = e.currentTarget.dataset.cnt; // 当前点击的答案下标，从0开始
      let dastr = e.currentTarget.dataset.dastr; // 选择的A-F
      this.xuanzeFunRun(cnt,dastr);
    },
    xuanzeFunRun(cnt,dastr){
      // 单选题
      if(this.data.nowPerQution.pq_type==1){
        var newXuanArr = [0,0,0,0,0,0];
        if(this.data.isXuanArr[cnt]==0){  // 如果没选择，则设置为选中，如果选中了，什么都做
          newXuanArr[cnt] = 1;
          this.setData({isXuanArr:newXuanArr});
        }
      }
      // 多选题
      else if(this.data.nowPerQution.pq_type==2){
        var newXuanArr = this.data.isXuanArr;
        if(this.data.isXuanArr[cnt]==1){  // 如果已经选中，则取消选择
          newXuanArr[cnt] = 0;
        }
        // 如果没选择，则继续让选中
        else{
          newXuanArr[cnt] = 1;
        }
        this.setData({isXuanArr:newXuanArr});
      }
      // 填空题
      else{
        var newXuanArr = [0,0,0,0,0,0];
        this.setData({isXuanArr:newXuanArr});
      }
      this.formatTip();
    },
    formatTip(){
      var tipStr = "";
      var _isxuanArr = this.data.isXuanArr;
      for(let i in _isxuanArr){
        if(_isxuanArr[i]==1){
          if(tipStr==""){
            tipStr = this.data.zimuArr[i];
          }
          else{
            tipStr += ','+this.data.zimuArr[i];
          }
        }
      }
      this.setData({xuanzeTip:tipStr})
    },
    /** 提交答案 */
    submitQuestion(){
      this.submitQuestionFun();
    },
    submitQuestionFun(tipSS){
      let _this = this;
      var cntStr = "";
      // 填空题的答案
      if(this.data.nowPerQution.pq_type==3){
        cntStr = this.data.tiankongDA;
      }
      // 单选和多选的答案
      else{
        for(let i in this.data.isXuanArr){
          if(this.data.isXuanArr[i]==1){
            var addI = parseInt(i)+1;
            cntStr=="" ? cntStr=addI : cntStr += ','+addI;
          }
        }
      }
      if(cntStr==""){
        var sss = tipSS ? tipSS : "请填写或选择答案选项";
        App.showError(sss)
      }
      else{
        // 传递答案，以及当前是第几个试题
        App._get('beganswer/submitDaAn', { daanStr:cntStr, 
          lastSecond:this.data.lastSecond,
          exid:this.data.perKaoObj.ex_id}, result => {
          //console.log(result);
          var tipArr = result.data.tipArr;
          var isRight = tipArr['isRight'];
          var rightCnt = parseInt(tipArr['passRightDaan'])-1;
          var rightZimu = _this.data.zimuArr[rightCnt];
          var isRightStr = isRight=='yes' ? '恭喜回答正确' : '回答错误，答案为：'+rightZimu;  
          // 已经全部回答完毕
          if(result.data.perQuestion == null){
            App.showError(isRightStr+"。答题结束，本次得分："+tipArr.getScore, function(){
              //如果是分享页面直接完成的答题
              if(_this.data.fromShare=='yes'){
                wx.reLaunch({
                  url: '/pages/user/index',
                });
              }
              else{
                wx.navigateBack({delta:1});
              }
            })
          }
          else{
            _this.setData({nowDaanTip:isRightStr, isRight:isRight, nextQaResult:result});
            // App.showError(isRightStr, function(){
            //   _this.opmTheQuestionData(result);
            // })
          }
        });
      }
    },
    /** 解析下一题，因为再提交本题答案的时候，已经将下一题数据传递回来了 */
    toNextQuestionFormat(){
      let _this = this;
      var result = this.data.nextQaResult; 
      _this.opmTheQuestionData(result);
    },
    /** 开始下一题 */
    toNextQuestion(){
      if(this.data.nextQaResult==null){
        this.submitQuestionFun('请填写或选择答案在进行下一题');
      }
      else{
        this.toNextQuestionFormat();
      }
    },
    /*** 倒计时结束 */
    timeOver(){
      let _this = this;
      App.showError("倒计时结束，确定进入下一题", function(){
        _this.xuanzeFunRun(0,'A');
        _this.setData({tiankongDA:'^'});  // 给出错误的答案
        _this.toNextQuestion();
      });
    },
    /** 绑定输入值 */
    getSearchContent: function(e) {
      this.data.tiankongDA = e.detail.value;
    },
});